package com.active4j.web.core.swagger2;

import io.swagger.annotations.ApiOperation;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

import static springfox.documentation.builders.PathSelectors.regex;

/**
 * @title Swagger2Config.java
 * @description swagger2相关配置
 * @time 2019年12月31日 上午9:02:03
 * @author 麻木神
 * @version 1.0
 */
@Configuration
@EnableSwagger2
@ConditionalOnProperty(name = "swagger.enable",  havingValue = "true")
public class Swagger2Config {


    /**
     * @return Docket
     * @description 基于包名分组生成API文档，还可以使用基于注解的
     * @author 麻木神
     * @time 2020年1月7日 上午10:29:20
     */
    @Bean
    public Docket createRestApiSys() {
//        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
//                // 为当前包路径
//                .apis(RequestHandlerSelectors.basePackage("com.active4j.web.system.controller")).paths(PathSelectors.any()).build().groupName("sys");

        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).forCodeGeneration(true)
                .select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .apis(RequestHandlerSelectors.any())
                .paths(regex("^.*(?<!error)$"))
                .build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());


    }

    /**
     * @return ApiInfo
     * @description swagger 文档信息
     * @author 麻木神
     * @time 2019年12月31日 上午9:17:54
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                // 设置文档标题(API名称)
                .title("active4j系统接口文件")
                // 文档描述
                .description("SpringBoot集成Swagger2构建RESTful接口")
                // 创建人信息
                .contact(new Contact("麻木神", "www.active4j.com", "chenxl@active4j.com"))
                // 服务条款URL
                .termsOfServiceUrl("http://127.0.0.1:8080/")
                // 版本号
                .version("1.0.0").build();
    }


    private List<ApiKey> securitySchemes() {
        List<ApiKey> apiKeyList = new ArrayList();
        apiKeyList.add(new ApiKey("x-auth-token", "x-auth-token", "header"));
        return apiKeyList;
    }

    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(regex("^(?!auth).*$"))
                        .build());
        return securityContexts;
    }

    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
        return securityReferences;


    }
}
